package io.geekidea.boot.system.controller;

import com.hwtx.form.controller.Api2Result;
import com.hwtx.form.domain.FormConstants;
import com.hwtx.form.domain.def.Select;
import com.hwtx.form.domain.repo.SysRoleService;
import com.hwtx.form.domain.service.RacService;
import com.hwtx.form.domain.service.ResourceService;
import com.hwtx.form.dto.RoleDataResourceDto;
import io.geekidea.boot.framework.response.ApiCode;
import io.geekidea.boot.system.dto.SysRoleDto;
import io.geekidea.boot.system.entity.SysRole;
import io.geekidea.boot.system.query.SysRoleQuery;
import io.geekidea.boot.system.vo.AmisTableData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 系统角色 控制器
 *
 * @author geekidea
 * @since 2022-12-26
 */
@Slf4j
@RestController
@Tag(name = "系统角色")
@RequestMapping("/admin/sysRole")
public class SysRoleController {

    @Resource
    RacService racService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private ResourceService resourceService;

    /**
     * 添加系统角色
     */
    @PostMapping("/addSysRole")
    @Operation(summary = "添加系统角色")
    public Api2Result addSysRole(@Valid @RequestBody SysRoleDto dto) {
        boolean flag = sysRoleService.addSysRole(dto);
        return Api2Result.result(flag);
    }

    /**
     * 修改系统角色
     */
    @PostMapping("/updateSysRole")
    @Operation(summary = "修改系统角色")
    public Api2Result updateSysRole(@Valid @RequestBody SysRoleDto dto) {
        boolean flag = sysRoleService.updateSysRole(dto);
        return Api2Result.result(flag);
    }

    /**
     * 删除系统角色
     */
    @PostMapping("/deleteSysRole/{id}")
    @Operation(summary = "删除系统角色")
    public Api2Result deleteSysRole(@PathVariable Long id) {
        boolean flag = sysRoleService.deleteSysRole(id);
        return Api2Result.result(flag);
    }

    /**
     * 系统所有角色列表
     */
    @PostMapping("/getSysRoleAllList")
    @Operation(summary = "获取角色列表")
    public Api2Result getSysRoleAllList(@Valid @RequestBody SysRoleQuery query) {
        List<SysRole> roles = sysRoleService.getSysRoleAllList(query);
        if (Objects.equals(query.getRetType(), FormConstants.RET_TYPE_OPTION)) {
            Select select = new Select();
            select.setOptions(roles.stream().map(role -> {
                Select.Option option = new Select.Option();
                option.setLabel(role.getName());
                option.setValue(role.getId().toString());
                return option;
            }).collect(Collectors.toList()));
            return Api2Result.result(ApiCode.SUCCESS, "加载成功", select);
        }
        AmisTableData<SysRole> misTableData = new AmisTableData<>();
        if ((query.getRetType() & FormConstants.RET_TYPE_LIST_INCLUDE_ORG_ASSIGNED) == FormConstants.RET_TYPE_LIST_INCLUDE_ORG_ASSIGNED) {
            misTableData.setValue(racService.getRolesByOrgId(query.getOrgId())
                    .stream().map(orgRole -> orgRole.getRoleId().toString()).collect(Collectors.joining(",")));
        }
        misTableData.setRows(roles);
        return Api2Result.success(misTableData);
    }

    /**
     * 获取角色权限ID集合
     */
    @PostMapping("/getMenuIdsByRoleId/{roleId}")
    @Operation(summary = "获取角色权限ID集合")
    public Api2Result getMenuIdsByRoleId(@PathVariable Long roleId) {
        List<Long> list = resourceService.getMenuIdsByRoleId(roleId, null);
        return Api2Result.success(list);
    }

    @GetMapping("/roleDataResource")
    @Operation(summary = "获取角色数据权限")
    public Api2Result getRoleDataResource(Long roleId) {
        return Api2Result.success(racService.getRoleDataResource(roleId));
    }

    @PostMapping("/roleDataSave")
    @Operation(summary = "新增/更新角色数据权限")
    public Api2Result roleDataAdd(@RequestBody RoleDataResourceDto roleDataResourceDto) {
        return Api2Result.success(racService.saveRoleDataResource(roleDataResourceDto));
    }
}
